<template>
  <div class="table-sty box-shadow">
    <div class="tabpane topcolor flexb othertop">
      <a v-for="item in menu" :key="item.value"
      @click="handleMenuActive(item.value)"
      :class="{ 'active': item.value === active }">{{$t(`jy.${item.name}`)}}</a>
      <div class="pr">
        <!-- <el-checkbox v-model="hidOtherKey">{{$t('jy.ycother')}}</el-checkbox> -->
        <a class="shuaxin" v-if="userLogin > 0"
        :class="{'act' : refresh}" @click="handleRefresh"><i class="el-icon-refresh"></i></a>
      </div>
    </div>
    <div class="trbtable pt">
      <el-table
        class="outTable"
        v-loading="loading"
        :data="tableData"
        :empty-text="!userLogin ? $t('jy.sqhlook') : $t('jy.noData')"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column :label="$t('order.time')" width="250">
          <template slot-scope="props">
            {{props.row.blockTime | dateFormatMonthHour}}
          </template>
        </el-table-column>
        <el-table-column :label="'RAM'" width="250">
          <template>
            RAM/EOS
          </template>
        </el-table-column>
        <el-table-column :label="$t('order.direction')" width="150">
          <template slot-scope="props">
            <span v-if="props.row.direction === 1" class="color-green">{{$t('order.buy')}}</span>
            <span v-if="props.row.direction === 2" class="color-red">{{$t('order.sell')}}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column width="150" :render-header="renderHeaderPrice">
          <template slot-scope="props">
            <span v-if="props.row.price">
              {{props.row.price}} <span class="fontx wordWrap" style="color: #999">EOS/KB</span>
            </span>
            <span class="nodataj" v-else>—</span>
          </template>
        </el-table-column> -->
        <el-table-column :label="$t('jy.jyed')" width="200">
          <template slot-scope="props">
            <span v-if="props.row.count">
              {{handleToFixed(props.row.count, 4)}} <span class="fontx wordWrap" style="color: #999">EOS</span>
            </span>
            <span class="nodataj" v-else>—</span>
          </template>
        </el-table-column>
        <!-- <el-table-column width="" :render-header="renderHeaderNumber">
          <template slot-scope="props">
            <span v-if="props.row.bytes">
              {{props.row.bytes}} <span class="fontx wordWrap" style="color: #999">KB</span>
            </span>
            <span style="color: #999" v-else>—</span>
          </template>
        </el-table-column> -->
        <el-table-column :label="'TrxID'" width="" style="text-align: right">
          <template slot-scope="props">
            <a class="trxidbox" :href="`https://bloks.io/tx/${props.row.trxId}`" target="_blank">{{props.row.trxId}}</a>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagebox center ptb" v-if="form.totalCount && $store.state.app.scatter.chain === 'eos'">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.currPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="form.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="form.totalCount">
        </el-pagination>
      </div>
    </div>
  </div>

</template>

<script>
import { toFixed } from '@/utils/validate';

export default {
  name: 'trading-bottom-history',
  data() {
    return {
      loading: false,
      refresh: false,
      userLogin: false,
      active: 0,
      menu: [
        {
          name: 'jyHistory',
          value: 0
        }
      ],
      form: {
        currPage: 1, // 当前页数
        pageSize: 10, // 每页条数
        totalCount: 0, // 总条数
        accountNo: '', // 当前账户
      },
      tableData: [],
    };
  },
  components: {
  },
  props: {
    refreshOrder: Boolean,
  },
  watch: {
    refreshOrder: function change() {
      this.handleGetUserOrder();
    },
    '$store.state.app.scatter.identity': {
      handler: function listen(val) {
        if (!val) {
          this.userLogin = false;
          return;
        }
        this.userLogin = true;
        this.form.accountNo = val.accounts[0].name;
        this.form.currPage = 1;
        this.handleGetUserOrder();
      },
      deep: true,
    },
  },
  created() {
  },
  mounted() {
    try {
      const identity = this.$store.state.app.scatter.identity;
      if (identity) {
        this.userLogin = true;
        this.form.accountNo = identity.accounts[0].name;
      }
    } catch (e) {
      console.log(e); // eslint-disable-line 
    }
    this.handleGetUserOrder();
  },
  methods: {
    renderHeaderNumber(h, { column, $index }) { // eslint-disable-line
      return (
        <div class="priceTip">
          <span>{this.$t('jy.number')}</span>
          <el-tooltip placement="bottom" effect="light">
            <div slot="content" class="priceTipContent" style="width: 280px;color: #999;font-size:12px;line-height: 24px;">
              <p>{this.$t('jy.ramNumberTip')}</p>
            </div>
            <div class="priceTipIcon">
              <i class="iconfont icon-huaban"></i>
            </div>
          </el-tooltip>
        </div>
      )
    },
    renderHeaderPrice(h, { column, $index }) { // eslint-disable-line
      return (
        <div class="priceTip">
          <span>{this.$t('jy.price')}</span>
          <el-tooltip placement="bottom" effect="light">
            <div slot="content" class="priceTipContent" style="width: 232px;color: #999;font-size:12px;line-height: 24px;">
              <p>{this.$t('jy.ramPriceTip')}</p>
            </div>
            <div class="priceTipIcon">
              <i class="iconfont icon-huaban"></i>
            </div>
          </el-tooltip>
        </div>
      )
    },
    // 数字截取 - 保留小数
    handleToFixed(num, p) {
      return toFixed(num, p);
    },
    // 菜单切换
    handleMenuActive(value) {
      this.active = value;
    },

    // 获取当前委托订单列表
    handleGetUserOrder() {
      const param = {
        account: this.form.accountNo,
        page: this.form.currPage,
        limit: this.form.pageSize,
      };
      if (!param.account) {
        return;
      }
      this.loading = true;
      this.$http.post('/eosRamAction/ramorders', param).then((res) => {
        this.loading = false;
        this.refresh = false;
        if (res.code === 401) {
          // 用户未登录
          this.userLogin = false;
          return;
        }
        if (res.code !== 0) {
          this.$message.error(res.msg);
          return;
        }
        this.userLogin = true;
        this.form.totalCount = res.page.totalCount;
        this.tableData = res.page.list;
        const scat = this.$store.state.app.scatter;
        if (scat.identity && scat.chain === 'eos') {
          this.tableData = res.page.list;
          return;
        }
        this.userLogin = false;
        this.tableData = [];
      });
    },
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.handleGetUserOrder();
    },
    handleCurrentChange(val) {
      this.form.currPage = val;
      this.handleGetUserOrder();
    },
    handleRefresh() {
      if (this.refresh) return;
      this.refresh = true;
      setTimeout(() => {
        this.handleGetUserOrder();
      }, 500);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/css/variables.scss";
$-clolor: #278eda;

.nodataj {
  display: inline-block;
}
.trxidbox {
  vertical-align: middle;
  display: inline-block;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  color: $-clolor !important;
}
.table-sty {
  margin-top: 10px;
}

.topcolor {
  background: $-topcolorf7;
}

.trbtable {
  background: #fff;
  padding: 0 25px;
}

.shuaxin {
  display: inline-block;
  margin: 0;
  font-size: 14px;
  padding: 0px 5px;

  &.act i{
    animation: rotating 1s linear infinite;
  }
}

.wordWrap{
  word-wrap: break-word;
  word-break: normal;
}

.theme-1B1D27 {
  .topcolor {
    background: $-topcolor2;
  }

  .trbtable {
    background: $-black1;
    /deep/ .cell {
      color: rgba(255, 255, 255, 0.6);
    }
  }
}
.tabpane /deep/.el-checkbox__label {
  color: #666;
  font-size: 12px;
}
.table-sty /deep/.el-table {
  .priceTip {
    line-height: 14px;
    margin-bottom: -4px;
    padding: 0;
    .priceTipIcon {
      line-height: 14px;
      vertical-align: middle;
      padding: 0;
      margin-left: 3px;
    }
  }
  font-size: 12px;
  .el-table__header-wrapper {
    font-size: 12px;
  }
  .el-table__empty-text {
    font-size: 12px;
    color: #666;
  }
  .cell {
    font-weight: normal;
  }
  .cell span{
    font-size: 12px;
  }
  .el-table__expand-icon {
    visibility: hidden;
  }
  .el-table__body tr .cell,
  .el-table__header tr .cell{
    text-align: center;
  }

  .el-table__body tr td:nth-child(2) .cell,
  .el-table__header tr th:nth-child(2) .cell,
  .el-table__body tr td:nth-child(4) .cell,
  .el-table__header tr th:nth-child(4) .cell,
  .el-table__body tr td:nth-child(5) .cell,
  .el-table__header tr th:nth-child(5) .cell,
  .el-table__body tr td:nth-child(6) .cell,
  .el-table__header tr th:nth-child(6) .cell, {
    text-align: right;
  }
  .el-table__header tr th:nth-child(6) .cell {
    padding-right: 50px;
  }
  .el-table__body tr td:nth-child(6) .cell {
    padding-right: 52px !important;
  }
  .el-table__body tr td:nth-child(3) .cell,
  .el-table__header tr th:nth-child(3) .cell {
    padding-left: 40px;
  }
  .el-table__body tr td:nth-child(2) .cell,
  .el-table__header tr th:nth-child(2) .cell {
    padding-right: 30px;
  }
  .el-table__body tr td:nth-child(4) .cell {
    padding-right: 12px;
  }
  .innerTable{
    .el-table__body tr td .cell,
    .el-table__header tr th .cell{
      text-align: right;
    }

    .el-table__body tr td:nth-child(1) .cell,
    .el-table__header tr th:nth-child(1) .cell{
      text-align: right;
    }

    .el-table__body tr td:nth-child(5) .cell,
    .el-table__header tr th:nth-child(5) .cell{
      padding-right: 50px;
    }
  }
}
</style>

